package com.irdstudio.apicenter.console.controller.authenticate;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;
import com.irdstudio.apicenter.console.controller.MacmsBaseAction;
import com.irdstudio.apicenter.console.dm.AppDuty;
import com.irdstudio.apicenter.console.dm.AppRole;
import com.irdstudio.apicenter.console.dm.AppUser;
import com.irdstudio.apicenter.console.service.authenticate.AppUserService;

/**
 * app应用管理
 * 
 * @author jr
 * 
 */
@Controller
@RequestMapping(value = "/appUser")
public class AppUserAction extends MacmsBaseAction {
	@Autowired
	private AppUserService appUserService;

	@RequestMapping(value = "addUser")
	public void addUser(@ModelAttribute("appUser") AppUser appUser,
			HttpServletRequest request, HttpServletResponse response)
			throws SQLException, ClassNotFoundException {
		int flag = -1;
		flag = appUserService.insertOne(appUser);
		Map<String, Object> map = new HashMap<String, Object>(1);
		map.put("flag", flag);
		String jsonResult = JSON.toJSONString(map);
		PrintWriter printWriter = null;
		try {
			printWriter = response.getWriter();
			printWriter.print(jsonResult);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (null != printWriter) {
				printWriter.flush();
				printWriter.close();
			}
		}
	}

	@RequestMapping(value = "jumpUpdateUser")
	public String jumpUpdateUser(HttpServletRequest request)
			throws SQLException, ClassNotFoundException {
		String appId = request.getParameter("appId");
		String userId = request.getParameter("userId");
		AppUser appUser = appUserService.queryAppTradePrivate(appId, userId);
		request.setAttribute("appUser", appUser);
		return "auth/updateUser";
	}

	@RequestMapping(value = "updateUser")
	public void updateUser(@ModelAttribute("appUser") AppUser appUser,
			HttpServletRequest request, HttpServletResponse response)
			throws SQLException, ClassNotFoundException {
		int flag = -1;
		flag = appUserService.update(appUser);
		Map<String, Object> map = new HashMap<String, Object>(1);
		map.put("flag", flag);
		String jsonResult = JSON.toJSONString(map);
		PrintWriter printWriter = null;
		try {
			printWriter = response.getWriter();
			printWriter.print(jsonResult);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (null != printWriter) {
				printWriter.flush();
				printWriter.close();
			}
		}
	}

	@RequestMapping(value = "jumpViewUser")
	public String jumpViewUser(HttpServletRequest request) throws SQLException,
			ClassNotFoundException {
		String appId = request.getParameter("appId");
		String userId = request.getParameter("userId");
		AppUser appUser = appUserService.queryAppTradePrivate(appId, userId);
		request.setAttribute("appUser", appUser);
		return "auth/queryUserDetail";
	}

	@RequestMapping(value = "setRolePage")
	public String setRolePage(HttpServletRequest request) throws Exception {
		String appId = request.getParameter("appId");
		String userid = request.getParameter("userid");
		if (StringUtils.isEmpty(userid)) {
			throw new Exception("必须传入应用标识(userid)参数!");
		}
		// 查询用户还没有的角色
		List<AppRole> arListNotYet = new ArrayList<AppRole>();
		arListNotYet = appUserService.queryAppRoleListNotYet(userid, appId);
		// 查询用户已经拥有的角色
		List<AppRole> arListAready = new ArrayList<AppRole>();
		arListAready = appUserService.queryAppRoleListAready(userid, appId);
		// 放入上下文
		request.setAttribute("appId", appId);
		request.setAttribute("userid", userid);
		request.setAttribute("arListNotYet", arListNotYet);
		request.setAttribute("arListAready", arListAready);
		return "auth/setRole";
	}

	@RequestMapping(value = "setJobPage")
	public String setJobPage(HttpServletRequest request) throws Exception {
		String appId = request.getParameter("appId");
		String userid = request.getParameter("userid");
		if (StringUtils.isEmpty(userid)) {
			throw new Exception("必须传入应用标识(userid)参数!");
		}
		// 查询用户还没有的岗位
		List<AppDuty> dtListNotYet = new ArrayList<AppDuty>();
		dtListNotYet = appUserService.queryAppDutyListNotYet(userid, appId);
		// 查询用户已经拥有的岗位
		List<AppDuty> dtListAready = new ArrayList<AppDuty>();
		dtListAready = appUserService.queryAppDutyListAready(userid, appId);
		// 放入上下文
		request.setAttribute("appId", appId);
		request.setAttribute("userid", userid);
		request.setAttribute("dtListNotYet", dtListNotYet);
		request.setAttribute("dtListAready", dtListAready);
		return "auth/setJob";
	}

	@RequestMapping(value = "saveUserRole")
	public void saveUserRole(HttpServletRequest request,
			HttpServletResponse response) throws SQLException,
			ClassNotFoundException {
		int flag = -1;

		String roleStr = request.getParameter("roleStr");
		String appid = request.getParameter("appid");
		String userid = request.getParameter("userid");

		flag = appUserService.insertUserRole(roleStr, appid, userid);
		Map<String, Object> map = new HashMap<String, Object>(1);
		map.put("flag", flag);
		String jsonResult = JSON.toJSONString(map);
		PrintWriter printWriter = null;
		try {
			printWriter = response.getWriter();
			printWriter.print(jsonResult);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (null != printWriter) {
				printWriter.flush();
				printWriter.close();
			}
		}
	}

	@RequestMapping(value = "saveUserDuty")
	public void saveUserDuty(HttpServletRequest request,
			HttpServletResponse response) throws SQLException,
			ClassNotFoundException {
		int flag = -1;

		String dutyStr = request.getParameter("dutyStr");
		String appid = request.getParameter("appid");
		String userid = request.getParameter("userid");

		flag = appUserService.insertUserDuty(dutyStr, appid, userid);
		Map<String, Object> map = new HashMap<String, Object>(1);
		map.put("flag", flag);
		String jsonResult = JSON.toJSONString(map);
		PrintWriter printWriter = null;
		try {
			printWriter = response.getWriter();
			printWriter.print(jsonResult);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (null != printWriter) {
				printWriter.flush();
				printWriter.close();
			}
		}
	}
}
